<!DOCTYPE html>

<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <title>Responsible Coding / Learn Vimscript the Hard Way</title>
        <meta name="description" content="">
        <meta name="author" content="Steve Losh">
        <!--[if lt IE 9]>
            <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->

        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

        <link href="/static/styles/skeleton/base.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/skeleton/skeleton.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/skeleton/layout.css" rel="stylesheet" type="text/css" />

        <link href="/static/styles/tango.css" rel="stylesheet" type="text/css" />
        <link href="/static/styles/style.less" rel="stylesheet/less" type="text/css" />

        <script type="text/javascript" src="/static/scripts/less.js"></script>
    </head>

    <body class="">
        <div class="container">
            <header class="sixteen columns">
                <h1><a href="/">Learn Vimscript the Hard Way</a></h1>
            </header>

            
    <section class="nav three columns">
        
<ul>
<li><a href="#responsible-coding">Responsible Coding</a><ul>
<li><a href="#commenting">Commenting</a></li>
<li><a href="#grouping">Grouping</a></li>
<li><a href="#short-names">Short Names</a></li>
<li><a href="#exercises">Exercises</a></li>
</ul>
</li>
</ul>


        <div class="prevnext">
            
                <a class="prev" href="17.html">&laquo; Prev</a>
            
            
                <a class="next" href="19.html">Next &raquo;</a>
            
        </div>
    </section>

    <section class="content twelve columns offset-by-one">
        <div> 
<h1 id="responsible-coding">Responsible Coding</h1>
<p>So far we've covered a bunch of Vim commands that let you customize Vim quickly.
All of them except for autocommand groups were single-line commands that you
can add to your <code>~/.vimrc</code> file in seconds.</p>
<p>In the next part of the book we're going to dive into Vimscript as a real
programming language, but before we do that I want to talk a bit about how to
stay sane while writing large amounts of Vimscript.</p>
<h2 id="commenting">Commenting</h2>
<p>Vimscript is extremely powerful, but has grown organically over the years into
a twisty maze ready to ensnare unwary programmers who enter it.</p>
<p>Options and commands are often terse and hard to read, and working around
compatibility issues can increase the complexity of your code.  Writing a plugin
and allowing for user customization introduces another entire layer above that!</p>
<p>Be defensive when writing anything that takes more than a few lines of
Vimscript.  Add a comment explaining what it does, and if there is a relevant
help topic, mention it in the comment!</p>
<p>This not only benefits you when you try to maintain it months or years later,
but also helps other people understand it if you share your <code>~/.vimrc</code> file on
Bitbucket or GitHub (which I highly recommend).</p>
<h2 id="grouping">Grouping</h2>
<p>Our mappings for editing and sourcing <code>~/.vimrc</code> have made it quick and easy to
add new things to it on the fly.  Unfortunately this also makes it easy for it
to grow out of control and become hard to navigate.</p>
<p>One way to combat this is to use Vim's code folding capabilities and group lines
into sections.  If you've never used Vim's folding you should look into it as
soon as you can.  Some people (myself included) find it indispensable in our day
to day coding.</p>
<p>First we need to set up folding for Vimscript files.  Add the following lines to
your <code>~/.vimrc</code> file:</p>
<pre class="codehilite"><code class="language-vim">augroup filetype_vim
    autocmd!
    autocmd FileType vim setlocal foldmethod=marker
augroup END</code></pre>


<p>This will tell Vim to use the <code>marker</code> method of folding for any Vimscript
files.</p>
<p>Go ahead and run <code>:setlocal foldmethod=marker</code> in the window with your
<code>~/.vimrc</code> file now.  Sourcing the file won't work, because Vim has already set
the FileType for this file and the autocommand only fires when that happens.  In
the future you won't need to do it manually.</p>
<p>Now add lines before and after that autocommand group so that it looks like
this:</p>
<pre class="codehilite"><code class="language-vim">" Vimscript file settings ---------------------- {{{
augroup filetype_vim
    autocmd!
    autocmd FileType vim setlocal foldmethod=marker
augroup END
" }}}</code></pre>


<p>Return to normal mode, put your cursor on any of those lines and type <code>za</code>.  Vim
will fold the lines starting at the one containing <code>{{{</code> and ending at the one
containing <code>}}}</code>.  Typing <code>za</code> again will unfold the lines.</p>
<p>You may think that adding explicit comments to source code that describe folding
is ugly at first.  I thought the same way when I first saw it.  For most files
I still think it's wrong.  Not everyone uses the same editor, so littering your
code with folding comments is just noisy to anyone else looking at the code in
something other than Vim.</p>
<p>Vimscript files are special case, though.  It's highly unlikely that someone who
doesn't use Vim will be reading your code, and it's especially important to
group things explicitly and thoughtfully when writing Vimscript so you don't go
crazy.</p>
<p>Try these explicit folds out for a while.  You might grow to love them.</p>
<h2 id="short-names">Short Names</h2>
<p>Vim allows you to use abbreviated names for most commands and options.  For
example, both of these commands do exactly the same thing:</p>
<pre class="codehilite"><code class="language-vim">:setlocal wrap
:setl wrap</code></pre>


<p>I'd like to <em>strongly</em> caution you against using these abbreviations in your
<code>~/.vimrc</code> file and in plugins that you write.  Vimscript is terse and cryptic
enough to begin with; shortening things further is only going to make it even
harder to read.  Even if <em>you</em> know what a certain short command means, someone
else reading your code might not.</p>
<p>With that said, the abbreviated forms are <em>great</em> for running commands manually
in the middle of coding.  No one will ever see them again after you press
return, so there's no reason to press more keys than you have to.</p>
<h2 id="exercises">Exercises</h2>
<p>Go through your entire <code>~/.vimrc</code> file and arrange the lines into related
groups.  Some places to start might be: "Basic Settings", "FileType-specific
settings", "Mappings", and "Status Line".  Add folding markers with headings to
each section.</p>
<p>Find out how to make Vim fold everything automatically the first time you open
the file.  Look at <code>:help foldlevelstart</code> for a good place to start.</p>
<p>Go through your <code>~/.vimrc</code> file and change any abbreviated commands and options
to their full names.</p>
<p>Look through your <code>~/.vimrc</code> file and make sure you don't have any sensitive
information inside.  Create a git or Mercurial repository, move the file into
it, and symlink that file to <code>~/.vimrc</code>.</p>
<p>Commit the repository you just made and put it on Bitbucket or GitHub so other
people can see it and get ideas for their own.  Be sure to commit and push the
repository fairly often so your changes are recorded.</p>
<p>If you use Vim on more than one machine, clone down that repository and symlink
the file there as well.  This will make it simple and easy to use the exact same
Vim configuration on all machines you work with.</p></div>

        <div class="prevnext">
            
                <a class="prev" href="17.html">&laquo; Previous</a>
            
            
                <a class="next" href="19.html">Next &raquo;</a>
            
        </div>
    </section>


            <footer class="sixteen columns">
                Made by <a href="http://stevelosh.com">Steve Losh</a>.

                <a href="/license.html">License</a>.

                Built with
                <a href="http://bitbucket.org/sjl/bookmarkdown/">Bookmarkdown</a>.
            </footer>
        </div>

        
            <script type="text/javascript">
                var _gaq = _gaq || [];
                _gaq.push(['_setAccount', 'UA-15328874-8']);
                _gaq.push(['_trackPageview']);

                (function() {
                 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                 })();
            </script>
        

        
            <script type="text/javascript">
                var _gauges = _gauges || [];
                (function() {
                 var t   = document.createElement('script');
                 t.type  = 'text/javascript';
                 t.async = true;
                 t.id    = 'gauges-tracker';
                 t.setAttribute('data-site-id', '4e8f83b7f5a1f546e200000d');
                 t.src = '//secure.gaug.es/track.js';
                 var s = document.getElementsByTagName('script')[0];
                 s.parentNode.insertBefore(t, s);
                 })();
             </script>
        
    </body>
</html>
